<template>
  <div class="echart">
    <div class="my-echarts" ref="echartsDivRef" :style="{ width: width, height: height }"></div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, watchEffect } from 'vue'
import { EChartsOption } from 'echarts'
import useEchart from '../hooks/useEchart'

interface echartProps {
  options: any
  width?: string
  height?: string
}
const props = withDefaults(defineProps<echartProps>(), {
  width: '100%',
  height: '360px'
})

const echartsDivRef = ref<HTMLElement>()

onMounted(() => {
  const { setOptions } = useEchart(echartsDivRef.value!)

  watchEffect(() => {
    // 如果option里面的数据发生改变 会重新渲染
    setOptions(props.options)
  })
})
</script>

<style lang="less" scoped>
.echart {
  width: 100%;
  height: 100%;
  .my-echarts {
    width: 100%;
    height: 100%;
  }
}
</style>
